//
//  SHConditionSelectedButton.m
//  youjian03
//
//  Created by Dadeso on 14/11/10.
//  Copyright (c) 2014年 com.shouhuobao. All rights reserved.
//

#import "SHConditionSelectedButton.h"

#define SH_SELECT_ICON_UPW 4.0f
#define SH_SELECT_ICON_H   20.0f
//容纳多选项文字内容的数组
static NSMutableArray * _selectedButtonArray = nil;

@implementation SHConditionSelectedButton

-(id)initWithButtonTag:(NSString *) buttonTag{
    if (self = [super init]) {
        //初始化
        _buttonTag = [buttonTag copy];
        [self setImage:[UIImage imageNamed:@"com_sec_normal"] forState:UIControlStateNormal];
        [self setImage:[UIImage imageNamed:@"com_sec_selected"] forState:UIControlStateSelected];
        [self addTarget:self action:@selector(selectedBtnClick) forControlEvents:UIControlEventTouchUpInside];
    }
    return self;
}

-(void)setHLight:(BOOL)hLight{
    
    _hLight = hLight;
    self.selected = hLight;
    
}

-(void)selectedBtnClick{
    
    self.selected = ! self.selected;
    _hLight = self.selected;
    
    //判断匿名评价的状态
    if ([_buttonTag isEqualToString:@"匿名评价"]) {
        if (self.selected == YES) {
            [_delegate SHAnonymousState:true];
        }else{
            [_delegate SHAnonymousState:false];
        }
    }
    
    //根据按钮状态将其文字添加到数组或从数组中移除
    if (self.selected == YES) {
        if(!_selectedButtonArray){
             _selectedButtonArray = [[NSMutableArray alloc] init];
        }
        if (!_selectedButtonOtherArray) {
            _selectedButtonOtherArray = [[NSMutableArray alloc] init];
        }
        [self addButtonToArray];
    }else{
        if (_selectedButtonArray.count != 0) {
            for (int i = 0; i < _selectedButtonArray.count; i++) {
                NSDictionary * dict = [_selectedButtonArray objectAtIndex:i];
                if ([[dict objectForKey:_buttonTag] length] > 0) {
                    [_selectedButtonArray removeObjectAtIndex:i];
                    [_delegate SHButtonDelegate:_selectedButtonArray];
                }else{
                    //return;
                }
            }
        }
    }
}

/*
 
 *  选项文字添加到数组中
 
 */
-(void)addButtonToArray{
    NSMutableDictionary * dict = [[NSMutableDictionary alloc] init];
    [dict setObject:self.titleLabel.text forKey:_buttonTag];
    HMLog(@"dic count is %d",_selectedButtonArray.count);
    if (_selectedButtonArray.count > 0) {
        for (int i = 0;i < _selectedButtonArray.count;i ++) {
            NSDictionary * dic = [_selectedButtonArray objectAtIndex:i];
            NSString * str = [dic objectForKey:_buttonTag];
            if (str.length > 0) {
                return;
            }else{
                if (i == _selectedButtonArray.count - 1) {
                    [_selectedButtonArray addObject:dict];
                    [_delegate SHButtonDelegate:_selectedButtonArray];
                }
            }
        }
    }else{
        [_selectedButtonArray addObject:dict];
        [_delegate SHButtonDelegate:_selectedButtonArray];
    }
}

/*
 
 **  设置点击按钮frame
 
 */
- (CGRect)imageRectForContentRect:(CGRect)contentRect {
    return SHRectMakeRound(0, SH_SELECT_ICON_UPW/2.0, SH_SELECT_ICON_H, SH_SELECT_ICON_H);
}

/*
 
 **  设置按钮右侧文字frame
 
 */
- (CGRect)titleRectForContentRect:(CGRect)contentRect {
    return CGRectMake(SH_SELECT_ICON_H + 9.0f, SH_SELECT_ICON_UPW,
                      CGRectGetWidth(contentRect) - SH_SELECT_ICON_H - 9.0f,
                      CGRectGetHeight(contentRect) - 2.0f * SH_SELECT_ICON_UPW);
}


@end
